using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using KickStart.Helpers;
using KickStart.Models;
using KickStart.Services;
using SubSonic.Repository;

namespace KickStart.Controllers
{
    //todo IMPORTANT: Make sure only admins can access this controller
    [Authorize]
    public class AdminController : BaseController
    {
        public AdminController()
            : this(null, null)
        {
        }

        public AdminController(IFormsAuthentication formsAuth, IMembershipService service)
        {
            FormsAuth = formsAuth ?? new FormsAuthenticationService();
            MembershipService = service ?? new AccountMembershipService();
        }

        public IFormsAuthentication FormsAuth
        {
            get;
            private set;
        }

        public IMembershipService MembershipService
        {
            get;
            private set;
        }

        public ActionResult Index()
        {
            //get users
            ViewData["users"] = Data.User.GetPaged(1, 20); 
            return View();
        }


        [JsonPox]
        public ActionResult GetUsers()
        {
            //get users
            ViewData.Model = Data.User.GetPaged(1, 20);
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [JsonPox]
        public ActionResult DeleteUser(long id)
        {
            try
            {
                Data.User.Delete(u=> u.UserId == id);
                ViewData.Model = new { success = true, id = id };
            }
            catch (Exception e)
            {
                ViewData.Model = new { success = false, id = id, message = e.Message };
            }
            return View();
        }
    }
}
